﻿@{
    ViewBag.Title = "Spjall";
}

<h2>Spjall</h2>

<div class="container">
    <input type="text" id="message" />
    <input type="button" id="sendmessage" value="Send" onkeypress="return runScript(event)" />
    <input type="hidden" id="displayname" />
    <ul id="discussion">
    </ul>
</div>

@section scripts {
    <!--Script references. -->
    <!--The jQuery library is required and is referenced by default in _Layout.cshtml. -->
    <!--Reference the SignalR library. -->
    <script src="~/Scripts/jquery.signalR-1.0.1.js"></script>

    <!--Reference the autogenerated SignalR hub script. -->
    <script src="~/signalr/hubs"></script>

    <!--SignalR script to update the chat page and send messages.--> 
    <script>
        $(function () {

            // Reference the auto-generated proxy for the hub.  
            var chat = $.connection.chatHub;
            var room = "public";

            // Create a function that the hub can call back to display messages.
            chat.client.addNewMessageToPage = function (name, message) {

                // Add the message to the page. 
                $('#discussion').append('<li><strong>' + htmlEncode(name) 
                    + '</strong>: ' + htmlEncode(message) + '</li>');
            };

            // Start the connection.
            $.connection.hub.start().done(function () {

                chat.server.join(room);

                $('#sendmessage').click(function () {

                    // Call the Send method on the hub. 
                    chat.server.send(room, "@User.Identity.Name", $('#message').val());

                    // Clear text box and reset focus for next comment. 
                    $('#message').val('').focus();
                });
                function runScript(e) {
                    if (e.keyCode == 13) {
                        var tb = document.getElementById("scriptBox");
                        eval(tb.value);
                        return false;
                    }
                }
            });
        });

        // This optional function html-encodes messages for display in the page.
        function htmlEncode(value) {
            var encodedValue = $('<div />').text(value).html();
            return encodedValue;
        }
    </script>
}